package com.perye.dokit.workcontroller;

import com.perye.dokit.aop.log.Log;
import com.perye.dokit.base.BaseController;
import com.perye.dokit.newservice.HqPhotoService;
import com.perye.dokit.newservice.HqVideoService;
import com.perye.dokit.newservice.HqWeddingTypesService;
import com.perye.dokit.newservice.PersionService;
import com.perye.dokit.utils.ArrayAssembly;
import com.perye.dokit.utils.DateUtils;
import com.perye.dokit.utils.StringUtils;
import com.perye.dokit.utils.pageData.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: chenhao
 * \* Date: 2021/7/19
 * \* Time: 11:13
 * \* Description: 基础信息模块
 * \
 */

@RestController
@Api(tags = "hq:人员管理")
@RequestMapping("/api/persion")
public class PersionController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(PersionController.class);

    @Autowired
    private PersionService service;

    @Autowired
    private HqPhotoService photoService;

    @Autowired
    private HqVideoService videoService;

    @Log("查询persion")
    @ApiOperation("查询persion")
    @GetMapping
    @PreAuthorize("@dokit.check('persion:list')")
    @ResponseBody
    public ResponseEntity<Object> list() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();

        try {
            ArrayAssembly.getPage(pageData);

            List<PageData> list = service.getList(pageData);

            result = ArrayAssembly.getDataToObject(list);


        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    @Log("新增persion")
    @ApiOperation("新增persion")
    @PostMapping
    @PreAuthorize("@dokit.check('persion:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody PageData pageData) throws Exception {
        pageData.put("createTime", DateUtils.getDate());
        Integer id =  service.create(pageData);
        //上传图片
        PageData imgData = new PageData(); //图片对象
        imgData.put("url", pageData.getString("url")); //图片url
        imgData.put("length", pageData.getString("length")); //图片length
        imgData.put("width", pageData.getString("width")); //图片width
        imgData.put("type", "persion"); //图片类型
        imgData.put("createTime", DateUtils.getDate());
        imgData.put("type_id", id);
        Integer pq_photo_id = photoService.create(imgData);
        //上传视频
        PageData videoData = new PageData(); //图片对象
        videoData.put("url", pageData.getString("videoUrl")); //图片url
        videoData.put("length", pageData.getString("vlength")); //图片length
        videoData.put("width", pageData.getString("vwidth")); //图片width
        videoData.put("createTime", DateUtils.getDate());
        videoData.put("type", "persion"); //图片类型
        videoData.put("typeId", id);
        videoService.create(videoData);
        List<Map<String, Object> > msList=  ( List<Map<String, Object>>)pageData.get("tableData");

        for (Map<String, Object> data : msList) {
            data.put("typeId",id);
            data.put("createTime", DateUtils.getDate());
        }
        service.addMiaoShuList(msList);

        return new ResponseEntity<>(pageData, HttpStatus.CREATED);
    }

    @Log("修改persion")
    @ApiOperation("修改persion")
    @PutMapping
    @PreAuthorize("@dokit.check('persion:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody PageData pageData) throws Exception {
        PageData imgData = new PageData(); //图片对象
        PageData videoData = new PageData(); //图片对象
        if(!StringUtils.isNull(pageData.get("photo_id"))){
            imgData.put("id", pageData.get("photo_id"));
            PageData img = photoService.findPhoto(imgData);
            if(!StringUtils.equals(img.getString("url"),pageData.getString("url")) ||
                    !StringUtils.equals(img.getString("length"),pageData.getString("length")) ||
                    !StringUtils.equals(img.getString("width"),pageData.getString("width"))){
                imgData.put("type","persion"); //类型
                imgData.put("url",pageData.getString("url"));
                imgData.put("length",pageData.getString("length"));
                imgData.put("width",pageData.getString("width"));
                photoService.update(imgData);
            }
        }
        if(!StringUtils.isNull(pageData.get("video_id"))){
            videoData.put("id", pageData.get("video_id"));
            PageData video = videoService.findVideo(videoData);
            if(!StringUtils.equals(video.getString("url"),pageData.getString("videoUrl")) ||
                    !StringUtils.equals(video.getString("length"),pageData.getString("vlength")) ||
                    !StringUtils.equals(video.getString("width"),pageData.getString("vwidth"))){
                videoData.put("type","persion"); //类型
                videoData.put("url",pageData.getString("videoUrl"));
                videoData.put("length",pageData.getString("vlength"));
                videoData.put("width",pageData.getString("vwidth"));
                videoService.update(videoData);
            }
        }
        List<Map<String, Object> > msList=  ( List<Map<String, Object>>)pageData.get("tableData");
        List<Map<String, Object> > newUpdatemsList= new ArrayList<Map<String, Object> >();
        List<Map<String, Object> > newAddmsList= new ArrayList<Map<String, Object> >();
        for (Map<String, Object> data : msList) {
            if(StringUtils.equals("update",data.get("type")+"")){
                newUpdatemsList.add(data);
            }
            if(StringUtils.equals("add",data.get("type")+"")){
                data.put("typeId",pageData.get("id"));
                data.put("createTime", DateUtils.getDate());
                newAddmsList.add(data);
            }
        }
        if(newUpdatemsList.size()>0){
            service.upDateMiaoShuList(newUpdatemsList);
        }

        if(newAddmsList.size()>0){
            service.addMiaoShuList(newAddmsList);
        }



        service.update(pageData);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除")
    @ApiOperation("删除")
    @PreAuthorize("@dokit.check('persion:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) throws Exception {
        service.deleteAll(ids);
        photoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("删除")
    @ApiOperation("删除")
    @RequestMapping("/delMiaoShu")
    public ResponseEntity<Object> delMiaoShu(@RequestBody PageData pageData) throws Exception {
        pageData.put("isDelete",1);
        service.deleteMiaoShu(pageData);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}